﻿#include "axiswidget.h"
#include <QGridLayout>
#include <QDebug>
AxisWidget::AxisWidget(QWidget *parent) : QWidget(parent)
{
    initGui();
    for(int i = 0; i< 8; i++)
    {
        connect(m_pPushButtonForward[i], &QPushButton::pressed, this, &AxisWidget::onMoveForward);
        connect(m_pPushButtonForward[i], &QPushButton::released, this, &AxisWidget::onMoveForwardStop);
        connect(m_pPushButtonBackward[i], &QPushButton::pressed, this, &AxisWidget::onMoveBackward);
        connect(m_pPushButtonBackward[i], &QPushButton::released, this, &AxisWidget::onMoveBackwardStop);
        connect(m_pPushButtonBackward[i], &QPushButton::released, this, &AxisWidget::onMoveBackwardStop);
        connect(m_pPushButtonClear[i], &QPushButton::clicked, this, &AxisWidget::onClear);
        connect(m_pPushButtonHome[i], &QPushButton::clicked, this, &AxisWidget::onGoHome);
    }
}


void AxisWidget::onMoveForward()
{
    QPushButton *p = dynamic_cast<QPushButton *>(sender());

    for(int axis = 0; axis < 8; axis++)
    {
        if(p == m_pPushButtonForward[axis])
        {
            int speed = m_pLineEditSpeed[axis]->text().toInt();
            int acc = m_pLineEditAcc[axis]->text().toInt();
            int dcc = m_pLineEditDcc[axis]->text().toInt();
            emit speedChanged(axis, speed);
            emit accChanged(axis, acc);
            emit decChanged(axis, dcc);
            emit do_jog(axis, 1);
            break;
        }
    }
}

void AxisWidget::onMoveForwardStop()
{
    QPushButton *p = dynamic_cast<QPushButton *>(sender());

    for(int axis = 0; axis < 8; axis++)
    {
        if(p == m_pPushButtonForward[axis])
        {
            emit do_stop(axis);
            break;
        }
    }
}

void AxisWidget::onMoveBackward()
{
    QPushButton *p = dynamic_cast<QPushButton *>(sender());

    for(int axis = 0; axis < 8; axis++)
    {
        if(p == m_pPushButtonBackward[axis])
        {
            double speed = m_pLineEditSpeed[axis]->text().toDouble();
            double acc = m_pLineEditAcc[axis]->text().toDouble();
            double dcc = m_pLineEditDcc[axis]->text().toDouble();
            emit speedChanged(axis, speed);
            emit accChanged(axis, acc);
            emit decChanged(axis, dcc);
            emit do_jog(axis, 0);
            break;
        }
    }
}

void AxisWidget::onMoveBackwardStop()
{
    QPushButton *p = dynamic_cast<QPushButton *>(sender());

    for(int axis = 0; axis < 8; axis++)
    {
        if(p == m_pPushButtonBackward[axis])
        {
            emit do_stop(axis);
            break;
        }
    }
}


void AxisWidget::initGui()
{
    QGridLayout *pGridLayout = new QGridLayout;

    QLabel * labelAxis = new QLabel;
    labelAxis->setText(u8"轴号");
    pGridLayout->addWidget(labelAxis, 0, 0, Qt::AlignHCenter);

    QLabel * labelEnable = new QLabel;
    labelEnable->setText(u8"使能");
    pGridLayout->addWidget(labelEnable, 0, 1, Qt::AlignHCenter);

    QLabel *labelSpeed = new QLabel;
    labelSpeed->setText(u8"速度");
    pGridLayout->addWidget(labelSpeed, 0, 2, Qt::AlignHCenter);

    QLabel *labelAcc = new QLabel;
    labelAcc->setText(u8"加速度");
    pGridLayout->addWidget(labelAcc, 0, 3, Qt::AlignHCenter);

    QLabel *labelDcc = new QLabel;
    labelDcc->setText(u8"减速度");
    pGridLayout->addWidget(labelDcc, 0, 4, Qt::AlignHCenter);

    QLabel *labelBackward = new QLabel;
    labelBackward->setText(u8"向后运动");
    pGridLayout->addWidget(labelBackward, 0, 5, Qt::AlignHCenter);

    QLabel *labelForward = new QLabel;
    labelForward->setText(u8"向前运动");
    pGridLayout->addWidget(labelForward, 0, 6, Qt::AlignHCenter);

    QLabel *labelTheoryPos = new QLabel;
    labelTheoryPos->setText(u8"理论位置");
    pGridLayout->addWidget(labelTheoryPos, 0, 7, Qt::AlignHCenter);

    QLabel *labelEncoderPos = new QLabel;
    labelEncoderPos->setText(u8"光栅尺读数");
    pGridLayout->addWidget(labelEncoderPos, 0, 8, Qt::AlignHCenter);

    QLabel *labelGoto = new QLabel;
    labelGoto->setText(u8"移动到");
    pGridLayout->addWidget(labelGoto, 0, 9, Qt::AlignHCenter);

    QLabel *labelPos = new QLabel;
    labelPos->setText(u8"目标点位");
    pGridLayout->addWidget(labelPos, 0, 10, Qt::AlignHCenter);

    QLabel *labelClear = new QLabel;
    labelClear->setText(u8"清零");
    pGridLayout->addWidget(labelClear, 0, 11, Qt::AlignHCenter);

    QLabel *labelHome = new QLabel;
    labelHome->setText(u8"回零");
    pGridLayout->addWidget(labelHome, 0, 12, Qt::AlignHCenter);

    QLabel *labelNl = new QLabel;
    labelNl->setText(u8"负限位");
    pGridLayout->addWidget(labelNl, 0, 13, Qt::AlignHCenter);

    QLabel *labelPl = new QLabel;
    labelPl->setText(u8"正限位");
    pGridLayout->addWidget(labelPl, 0, 14, Qt::AlignHCenter);


    QLabel *labelHm = new QLabel;
    labelHm->setText(u8"原点");
    pGridLayout->addWidget(labelHm, 0, 15, Qt::AlignHCenter);


     for(int i = 0; i < 8; i++)
    {
        QLabel *labelAxis = new QLabel(this);
        labelAxis->setText(QString(u8"轴 %1").arg(i + 1));
        pGridLayout->addWidget(labelAxis, i + 1, 0, Qt::AlignHCenter);

        m_pCheckBoxEnable[i] = new QCheckBox(this);
        pGridLayout->addWidget(m_pCheckBoxEnable[i] , i + 1, 1);

        m_pLineEditSpeed[i] = new QLineEdit(this);
        m_pLineEditSpeed[i]->setText("6400");
        pGridLayout->addWidget(m_pLineEditSpeed[i] , i + 1, 2);

        m_pLineEditAcc[i] = new QLineEdit(this);
        m_pLineEditAcc[i]->setText("64000");
        pGridLayout->addWidget(m_pLineEditAcc[i] , i + 1, 3);

        m_pLineEditDcc[i] = new QLineEdit(this);
        m_pLineEditDcc[i]->setText("64000");
        pGridLayout->addWidget(m_pLineEditDcc[i] , i + 1, 4);

        m_pPushButtonBackward[i] = new QPushButton(this);
        m_pPushButtonBackward[i]->setText("<-");
        pGridLayout->addWidget(m_pPushButtonBackward[i] , i + 1, 5);

        m_pPushButtonForward[i] = new QPushButton(this);
        m_pPushButtonForward[i]->setText("->");
        pGridLayout->addWidget(m_pPushButtonForward[i] , i + 1, 6);

        m_pLabelTheoryPos[i] = new QLabel(this);
        m_pLabelTheoryPos[i]->setText("0");
        m_pLabelTheoryPos[i]->setFrameShape(QLabel::Box);
        pGridLayout->addWidget(m_pLabelTheoryPos[i] , i + 1, 7);

        m_pLabelEncoderPos[i] = new QLabel(this);
        m_pLabelEncoderPos[i]->setText("0");
        m_pLabelEncoderPos[i]->setFrameShape(QLabel::Box);
        pGridLayout->addWidget(m_pLabelEncoderPos[i] , i + 1, 8);

        m_pPushButtonGoto[i] = new QPushButton(this);
        m_pPushButtonGoto[i]->setText("Goto");
        pGridLayout->addWidget(m_pPushButtonGoto[i] , i + 1, 9);


        m_pLineEditTargetPos[i] = new QLineEdit(this);
        m_pLineEditTargetPos[i]->setText("0");
        pGridLayout->addWidget(m_pLineEditTargetPos[i] , i + 1, 10);

        m_pPushButtonClear[i] = new QPushButton(this);
        m_pPushButtonClear[i]->setText("Clear");
        pGridLayout->addWidget(m_pPushButtonClear[i] , i + 1, 11);

        m_pPushButtonHome[i] = new QPushButton(this);
        m_pPushButtonHome[i]->setText("Home");
        pGridLayout->addWidget(m_pPushButtonHome[i] , i + 1, 12);

        m_pCheckBoxNLimit[i] = new QCheckBox;
        m_pCheckBoxNLimit[i]->setCheckable(true);
        m_pCheckBoxNLimit[i]->setEnabled(false);
        pGridLayout->addWidget(m_pCheckBoxNLimit[i], i + 1, 13);

        m_pCheckBoxPLimit[i] = new QCheckBox;
        m_pCheckBoxPLimit[i]->setCheckable(true);
        m_pCheckBoxPLimit[i]->setEnabled(false);
        pGridLayout->addWidget(m_pCheckBoxPLimit[i], i + 1, 14);

        m_pCheckBoxHome[i] = new QCheckBox;
        m_pCheckBoxHome[i]->setCheckable(true);
        m_pCheckBoxHome[i]->setEnabled(false);
        pGridLayout->addWidget(m_pCheckBoxHome[i], i + 1, 15);

    }
     setLayout(pGridLayout);
}

void AxisWidget::setPos(int32_t theoryPos[], int32_t encoderPos[])
{
    for(int axis = 0; axis < 4; axis ++)
    {
//        m_encoderPos[axis] = encoderPos[axis];

        m_pLabelTheoryPos[axis]->setNum(theoryPos[axis]);
        m_pLabelEncoderPos[axis]->setNum(encoderPos[axis]);
    }
}

void AxisWidget::setLimitStatus(unsigned short axis, bool llimit, bool rlimit, bool home)
{
    qDebug() << "setLimitStatus: axis = " << axis << ", llimit =" << llimit << ", rlimit = " << rlimit;
    m_pCheckBoxPLimit[axis]->setChecked(llimit);
    m_pCheckBoxNLimit[axis]->setChecked(rlimit);
    m_pCheckBoxHome[axis]->setChecked(home);
}

void AxisWidget::onGoto()
{
    QPushButton *p = dynamic_cast<QPushButton *>(sender());
    for(int axis = 0; axis < 8; axis++)
    {
        if(p == m_pPushButtonGoto[axis])
        {
            int pos = m_pLineEditTargetPos[axis]->text().toInt();
            int speed = m_pLineEditSpeed[axis]->text().toInt();
            int acc = m_pLineEditAcc[axis]->text().toInt();
            int dcc = m_pLineEditDcc[axis]->text().toInt();
            emit speedChanged(axis, speed);
            emit accChanged(axis, acc);
            emit decChanged(axis, dcc);
            emit moveTo(axis, pos);
            break;
        }
    }

}

void AxisWidget::onAbort()
{
    emit do_abort();
}

void AxisWidget::onClear()
{
    QPushButton *p = dynamic_cast<QPushButton *>(sender());
    for(int axis = 0; axis < 8; axis++)
    {
        if(p == m_pPushButtonClear[axis])
        {
            emit do_zero(axis, 0);
            break;
        }
    }
}

void AxisWidget::onGoHome()
{
    QPushButton *p = dynamic_cast<QPushButton *>(sender());
    for(int axis = 0; axis < 8; axis++)
    {
        if(p == m_pPushButtonHome[axis])
        {
            emit do_home(axis);
            break;
        }
    }
}
